package com.tyc.threalpool;

import com.google.common.util.concurrent.ThreadFactoryBuilder;

import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * 调整线程池 使得达到线程核心数后优先创建线程使其达到最大线程后再入队
 */
public class Demo03 {
    public static void main(String[] args) {
        ThreadFactory threadFactory = new ThreadFactoryBuilder().setNameFormat("consumer-pool-%d").build();
        TaskQueue taskQueue = new TaskQueue(100);
        CustomThreadPoolExecutor executor = new CustomThreadPoolExecutor(3,
                10,
                3,
                TimeUnit.MILLISECONDS,
                taskQueue,
                threadFactory,
                new ThreadPoolExecutor.CallerRunsPolicy());
        taskQueue.setThreadPoolExecutor(executor);
        for (int i = 0; i < 100; i++) {
            System.out.println("poolSize"+executor.getPoolSize());
            System.out.println("queueSize"+executor.getQueue().size());
            executor.execute(()->{
                try {
                    TimeUnit.SECONDS.sleep(30);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            });
        }

    }

}
